/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 8/1/18
Time: 4:41 AM
 */

package com.ug_project.dict.engine.indextable

class AddressBlock(indexTable: IndexTable, id: Int, val level: Int) : Block(indexTable, id), Iterable<AddressBlock> {

    val addrs = IntArray(4)

    init {
        buffer.apply {
            rewind()
            for (i in 0 until addrs.size) {
                addrs[i] = int
            }
        }
    }

    operator fun set(index: Int, value: Int){
        addrs[index] = value
    }
    operator fun get(index: Int): Int = addrs[index]

    override fun writeToChannel() = autoWriteToChannel {
        for (i in 0 until addrs.size) {
            putInt(addrs[i])
        }
    }
    override fun iterator(): Iterator<AddressBlock> = AddressBlockIterator(this)

    val AvailableAddrs: Array<Int>
    get() {
        if(level > 1) throw Exception("Level must be 1")
        return addrs.filter { it > 0 }.toTypedArray()
    }
}